#This script is run as follows: python logparse.py <time_delta>
# And it parses all the .txt files in the current directory and in all subdirectories. 
# It prints the name of the file and then the list of  lines that has a timestamp <time_delta> 
# greater than the timestamp of the previous line. 

import sys
import re
import os.path
topdir = '.'
exten = '.txt'

def hmsm_to_milliseconds(t):
    h, m, s, ms = [int(i) for i in t.split(':')]
    return 3600000*h + 60000*m + 1000*s + ms


for root, subFolders, files in os.walk(topdir):
	counter = 0
	max_delta = sys.argv[1]
	#print max_delta
	ext = exten.lower()
	
	for name in files:
		print_file_flag = 1
		if name.lower().endswith(ext):
			full_filename = os.path.join(root, name)
			f = open(full_filename,'r')
			for line in f.readlines():
				info = line.split()
				try:
					timeval = re.sub('^.*T\[','',info[0])
					timeval = re.sub(']','',timeval)
					try:
						timeval= hmsm_to_milliseconds(timeval)
					except:
						f.close()
					if counter > 0:
						try:
							if (int(timeval) - int(timeval_old)) > int(max_delta):
								if print_file_flag == 1:
									print full_filename
									print_file_flag = 0
								print "%s" %line
						except:
							f.close()
					counter = counter + 1
					timeval_old = timeval
				except IndexError:
					f.close()

